package now;

import java.io.IOException;
import java.util.Enumeration;
import java.util.Random;

import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;

import playlist.Crawler;

import tool.FileConnectionUtils;
import tool.Path;

import function.Mp3File;

public class NowManager {

	public static Mp3File selectNewMp3FromBoot() {
		String mp3Name = null;
		try {
			FileConnection linkConn = (FileConnection)
				Connector.open(Path.NOW);
			FileConnection connection = (FileConnection) Connector.open(Path.BOOT);
			Enumeration mp3s = connection.list("*.mp3", true);
			while (linkConn.exists()) {
				mp3Name = (String) mp3s.nextElement();
				linkConn = (FileConnection) Connector.open(
						Path.NOW + '/' + mp3Name.substring(0, 
								mp3Name.length() - 4) + ".link"
				);
				
			}
			linkConn.create();
			linkConn.close();
			connection.close();
		} catch (IOException e) {
			Crawler.alert(e);
		}
		return new Mp3File(Path.BOOT, mp3Name);
	}

	public static int getBootSize() {
		return FileConnectionUtils.getNumMp3(Path.BOOT)
			- FileConnectionUtils.getNum(Path.NOW, "*.link")
			- FileConnectionUtils.getNum(Path.NOW, "*.delete");
	}
}
